#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e5 + 5;

mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());
int n, m;
int a[N];
int lowbit(int x) { return x & -x; }
int highbit(int x) {
  while (x != lowbit(x)) {
    x -= lowbit(x);
  }
  return x;
}
void f(int r) {
  if (r < 0) return;
  if (r == 0) {
    a[0] = 0;
    return;
  }
  int x = highbit(r);
  int l = 2 * x - 1 - r;
  rep(i, l, r) a[i] = l + r - i;
  f(l - 1);
}
void solve() {
  int l, r;
  cin >> l >> r;
  f(r);
  ll sum = 0;
  rep(i, l, r) sum += i | a[i];
  cout << sum << endl;
  rep(i, l, r) cout << a[i] << ' ';
  cout << endl;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}